package com.simonall.utils;

import java.io.Serializable;

import com.simonall.enums.ResponseCodeEnum;
import com.simonall.i18n.MessageSource;

import cn.hutool.json.JSONUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * Response
 * 
 * @author simonall
 */
@Getter
@NoArgsConstructor
public class Response<T> implements Serializable {

	private static final long serialVersionUID = 3126376952008717753L;

	// 业务码
	private String code;

	// 业务描述
	private String desc;

	// 结果
	private T data;

	public Response(String code, String desc, T data) {
		this.code = code;
		this.desc = desc;
		this.data = data;
	}

	public Response<T> setCode(String code) {
		this.code = code;
		return this;
	}

	public Response<T> setMessage(String desc) {
		this.desc = desc;
		return this;
	}

	public Response<T> setData(T data) {
		this.data = data;
		return this;
	}

	public static <T> Response<T> build(String code, String desc) {
		return new Response<T>(code, desc, null);
	}

	public static <T> Response<T> build(ResponseCodeEnum responseCode) {
		String code = responseCode.name();
		String desc = MessageSource.getMessage(responseCode);
		return new Response<T>(code, desc, null);
	}

	public String toJson() {
		return JSONUtil.toJsonStr(this);
	}

}
